//
//  TabBarView.m
//  TabBarView
//
//  Created by HaiSheng on 15/8/8.
//  Copyright (c) 2015年 huanghaisheng. All rights reserved.
//

#import "TabBarView.h"

@implementation TabBarView

- (void)dealloc
{
    self.delegate = nil;
}

- (id)initWithFrame:(CGRect)frame Items:(NSArray *)items BarStyle:(TabItemStyle)barStyle SelectedIndex:(NSInteger)selectedIndex
{
    
    if (self = [super initWithFrame:frame]) {
        
        self.tabBarInfos = items;
        selectedIndex = [self validateIndex:selectedIndex];
        
        NSInteger itemCount = items.count;
        float itemWidth = frame.size.width / itemCount;
        
        //加顶部灰线
        _topLineView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.width, 0.5)];
        _topLineView.backgroundColor = [UIColor colorWithWhite:216.0/255.0 alpha:1.0];
        [self addSubview:_topLineView];
        
        if (itemCount > 0) {
            
            for (int i = 0; i < items.count; i++) {
                TabBarItemView *itemView = nil;
                id tmp = items[i];
                if ([tmp isKindOfClass:[TabBarItemView class]]) {
                    itemView = items[i];
                } else {
                    
                    NSDictionary *dictItem = items[i];
                    itemView = [[TabBarItemView alloc] initWithFrame:CGRectZero
                                                            ItemInfo:dictItem
                                                               Style:barStyle];
                }
                
                
                CGRect itemFrame = itemView.frame;
                itemFrame.origin.x = itemWidth * i;
                itemFrame.origin.y = 0.0;
                itemFrame.size.width = itemWidth;
                itemFrame.size.height = frame.size.height;
                itemView.frame = itemFrame;
                itemView.delegate = self;
                itemView.tag = i;
                
                [self addSubview:itemView];
                
                if (i == selectedIndex) {
                    self.selectedTabBarItem = itemView;
                    [self.selectedTabBarItem setHighlightedStatus];
                }
                
            }
        }
        
    }
    
    return self;
}

- (NSInteger)validateIndex:(NSInteger)index
{
    if (index < 0 || index >= self.tabBarInfos.count) {
        index = 0;
    }
    return index;
}

#pragma mark - TabBarItemViewDelegate
- (BOOL)shouldClickedTabBarItemView:(TabBarItemView *)view
{
    if ([view isEqual:self.selectedTabBarItem]) {
        return NO;
    } else {
        if ([self.delegate respondsToSelector:@selector(tabBarView:shouldSelectAtIndex:)]) {
            return [self.delegate tabBarView:self shouldSelectAtIndex:view.tag];
        } else {
            return YES;
        }
    }
}

- (void)didClickedTabBarItemView:(TabBarItemView *)view
{
    if (![view isEqual:self.selectedTabBarItem]) {
        [self.selectedTabBarItem setNormalStatus];
        self.selectedTabBarItem = view;
        [self.delegate tabBarView:self didSelectAtIndex:view.tag];
        
    }
    
}


- (void)setBarItemHighlightedAtIndex:(NSInteger)index {
    
    NSArray *subArray = [self subviews];
    for (TabBarItemView *itemView in subArray) {
        if ([itemView isKindOfClass:[TabBarItemView class]]) {
            if (itemView.tag == index) {
                [itemView setHighlightedStatus];
                self.selectedTabBarItem = itemView;
            } else {
                [itemView setNormalStatus];
            }
        }
    }
}

- (void)clickAtIndex:(NSInteger)index
{
    NSArray *subArray = [self subviews];
    for (TabBarItemView *itemView in subArray) {
        if ([itemView isKindOfClass:[TabBarItemView class]]) {
            if (itemView.tag == index) {
                [itemView clickMe];
            }
        }
        
    }
}

@end
